﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace GoShopping.Web.ProductManager
{
    public partial class RelatedPrdt : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetDDLPrdtType();
                BindBoth();
                GetGVRelated();
            }
        }

        private void BindBoth()
        {
            BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
            IList<Model.Product> il = pb.GetAllProduct();
            rblMain.DataSource = il;
            rblMain.DataBind();

            cblVice.DataSource = il;
            cblVice.DataBind();
        }
        void GetDDLPrdtType()
        {
            SetControlCommon.GetDDLType(ddlPrdtType);
            SetControlCommon.GetDDLType(ddlPrdt1Type);
        }

        protected void ddlPrdtType_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindRadioButton(ddlPrdtType);
        }

        //次关联表的商品类型DDL
        protected void ddlPrdt1Type_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindCheckBox(ddlPrdt1Type);
        }

        //绑定商品
        protected void btnBindPrdt_Click(object sender, EventArgs e)
        {
            if (rblMain.SelectedIndex == -1)
            {
                return;
            }
            BLL.SysInfo.RelatedPrdctBean rpb = new GoShopping.BLL.SysInfo.RelatedPrdctBean();
            Model.RelatedPrdct rp;
            foreach (ListItem li in cblVice.Items)
            {
                if (li.Selected)
                {
                    rp = new GoShopping.Model.RelatedPrdct();
                    rp.BeRelatedPrdctID = Convert.ToInt32(rblMain.SelectedValue);
                    rp.TobeRelatedPrdctID = Convert.ToInt32(li.Value);
                    rpb.Add(rp);
                }
            }
            GetGVRelated();
        }

        private void GetGVRelated()
        {
            gvRelated.DataSource = new BLL.SysInfo.RelatedPrdctBean().GetAll();
            gvRelated.DataBind();
        }
        private void BindRadioButton(DropDownList ddl)
        {
            if (ddl != null)
            {
                BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
                IList<Model.Product> il = pb.GetProductList(" AND ProductTypeID=" + ddl.SelectedValue);
                rblMain.DataSource = il;
                rblMain.DataBind();
            }
            else
            {
                BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
                IList<Model.Product> il = pb.GetAllProduct();
                rblMain.DataSource = il;
                rblMain.DataBind();
            }
        }

        private void BindCheckBox(DropDownList ddl)
        {
            if (ddl != null)
            {
                BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
                IList<Model.Product> il = pb.GetProductList(" AND ProductTypeID=" + ddl.SelectedValue);
                cblVice.DataSource = il;
                cblVice.DataBind();
            }
            else
            {
                BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
                IList<Model.Product> il = pb.GetAllProduct();
                cblVice.DataSource = il;
                cblVice.DataBind();
            }
        }

        //void GetLBPrdtByType(DropDownList ddl)
        //{
        //    BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
        //    IList<Model.Product> il = pb.GetAllProduct();
        //    if (ddl.ID == "ddlPrdtType")
        //    {
        //        lbMain.Items.Clear();
        //    }
        //    else if (ddl.ID == "ddlPrdt1Type")
        //    {
        //        lbVice.Items.Clear();
        //    }

        //    foreach (Model.Product pd in il)
        //    {
        //        if (pd.ProductTypeID.ToString() == ddl.SelectedValue)
        //        {
        //            if (ddl.ID == "ddlPrdtType")
        //            {
        //                lbMain.Items.Add(new ListItem(pd.Name, pd.ID.ToString()));
        //            }
        //            else if (ddl.ID == "ddlPrdt1Type")
        //            {
        //                lbVice.Items.Add(new ListItem(pd.Name, pd.ID.ToString()));
        //            }
        //        }
        //    }
        //    //}
        //}

        //主关联表的商品类型DDL
        protected void gvRelated_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRelated.PageIndex = e.NewPageIndex;
            GetGVRelated();
        }
        protected void gvRelated_RowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        public string GetPrdtName(object obj)
        {
            BLL.ProductBean pb = new GoShopping.BLL.ProductBean();
            return pb.GetProduct(Convert.ToInt32(obj)).Name;
        }
        protected void btnDelRelationShip_Click(object sender, EventArgs e)
        {
            Model.RelatedPrdct rp;
            BLL.SysInfo.RelatedPrdctBean rpb = new GoShopping.BLL.SysInfo.RelatedPrdctBean();
            // 遍历数据集
            foreach (GridViewRow row in this.gvRelated.Rows)
            {
                // 获取行首的CheckBox，判断是否为Checked
                CheckBox cb = (CheckBox)row.FindControl("cbSingle");
                if (cb.Checked == false)
                {
                    continue;
                }
                // 若为Checked，将信息添加到ArrayList
                rp = new GoShopping.Model.RelatedPrdct();
                rp.BeRelatedPrdctID = Convert.ToInt16(((Label)row.FindControl("lblBe")).Text);
                rp.TobeRelatedPrdctID = Convert.ToInt16(((Label)row.FindControl("lblToBe")).Text);
                rpb.Del(rp);
            }
            GetGVRelated();
        }
        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            // 遍历数据集的所有数据项
            foreach (GridViewRow row in this.gvRelated.Rows)
            {
                // 取得数据项行首的CheckBox，使其Checked属性与表头的CheckBox同步
                CheckBox subCB = (CheckBox)row.FindControl("cbSingle");
                subCB.Checked = ((CheckBox)sender).Checked;
            }
        }
}
}
